package cmd

import (
	"fmt"

	initCmd "gitee.com/shiwjlinux/arm/cmd/init"
	"gitee.com/shiwjlinux/arm/cmd/start"
	"gitee.com/shiwjlinux/arm/conf"
	"gitee.com/shiwjlinux/arm/ioc"
	"github.com/spf13/cobra"

	_ "gitee.com/shiwjlinux/arm/apps"
)

var configPath string

var RootCmd = &cobra.Command{
	Use:   "arm",
	Short: "arm service",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) > 0 {
			if args[0] == "version" {
				fmt.Println("v0.0.1")
			}
			cmd.Help()
		}
	},
}

func Execute() error {
	cobra.OnInitialize(func() {
		// 加载配置
		cobra.CheckErr(conf.LoadConfigFromYaml(configPath))
		// 初始化IOC
		cobra.CheckErr(ioc.Controller.Init())
		// 初始化API
		cobra.CheckErr(ioc.Api.Init())
	})

	return RootCmd.Execute()
}

func init() {
	RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "etc/application.yaml", "config file (default is etc/application.yaml)")
	RootCmd.AddCommand(initCmd.Cmd)
	RootCmd.AddCommand(start.Cmd)
}
